<?php
/**
 * Created by PhpStorm.
 * Loan: Administrator
 * Date: 2020/3/6 0006
 * Time: 14:58
 */

namespace app\pcapi\controller;


use think\Controller;
use think\facade\Hook;

//基类控制器
class Base extends Controller
{
    /**
     * @return bool|void
     * @author ASUS
     * @date 2020-08-25 10:25
     * 基类控制器
     * 跨域验证，token验证，权限验证
     */
    public function initialize()
    {
        header('Content-Type: text/html;charset=utf-8');// *代表允许任何网址请求
        header('Access-Control-Allow-Origin:*');
        header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept,USER_ID,TOKEN");
        header("Access-Control-Allow-Methods:HEAD, GET, POST, DELETE, PUT, OPTIONS");
        $controller = $this->request->controller();
        //不需要验证权限的控制器
        $commonController = ["Login","Common"];
        if(!in_array($controller,$commonController)){
            /**验证token */
            $token = input('token');//客户端token
            if(empty($token)) {//客户端token不存在
                api_return(203,'token不能为空',[]);
            }
            $admin = input('admin');
            if(empty($admin)) {//客户端未传登陆名
                api_return(203,'用户名不能为空',[]);
            }
            $cache = cache($admin);
            if(empty($cache)){
                api_return(201, '请重新登录',[]);
            }
            if(cache($admin)['token']!=$token) {//客户端token不存在
                api_return(201, 'token失效，请重新登录',[]);
            }
        }
    }
}